<template>
  <!-- 添加或修改参数配置对话框 -->
  <el-dialog :title="title" :visible.sync="visible" width="500px" append-to-body :close-on-click-modal="false" @closed="reset">
    <el-form ref="form" :model="form" :rules="rules" label-width="80px">
      <el-form-item label="字典名称" prop="dictName">
        <el-input v-model="form.dictName" placeholder="请输入字典名称"/>
      </el-form-item>
      <el-form-item label="字典类型" prop="dictType">
        <el-input v-model="form.dictType" placeholder="请输入字典类型"/>
      </el-form-item>
      <el-form-item label="状态" prop="status">
        <el-radio-group v-model="form.status">
          <el-radio
            v-for="dict in statusOptions"
            :key="dict.dictValue"
            :label="dict.dictValue">
            {{dict.dictLabel}}
          </el-radio>
        </el-radio-group>
      </el-form-item>
      <el-form-item label="备注" prop="remark">
        <el-input v-model="form.remark" type="textarea" placeholder="请输入内容"></el-input>
      </el-form-item>
    </el-form>
    <div slot="footer" class="dialog-footer">
      <el-button type="primary" :loading="submitting" @click="submitForm">确 定</el-button>
      <el-button @click="close">取 消</el-button>
    </div>
  </el-dialog>
</template>

<script>
import { getType, updateType, addType } from "@/api/system/dict/type";

export default {
  name: "typeEdit",
  props: {
    statusOptions: {
      type: Array,
      default: []
    }
  },
  data() {
    return {
      id: undefined,
      title: "新增编辑",
      visible: false,
      submitting: false,
      // 表单参数
      form: {
        dictName: undefined,
        dictType: undefined,
        status: "0",
        remark: undefined
      },
      // 表单校验
      rules: {
        dictName: [
          { required: true, message: "字典名称不能为空", trigger: "blur" }
        ],
        dictType: [
          { required: true, message: "字典类型不能为空", trigger: "blur" }
        ]
      }
    }
  },
  methods: {
    init(id, title) {
      this.visible = true;
      this.id = id;
      this.title = title;
      if (id) {
        this.initData();
      }
    },
    initData() {
      getType(this.id).then(response => {
        this.form = response;
      });
    },
    close() {
      this.visible = false;
    },
    submitForm() {
      this.$refs["form"].validate(valid => {
        if (valid) {
          this.submitting = true;
          if (this.id) {
            updateType(this.id, this.form).then(response => {
              this.submitting = false;

              this.msgSuccess("修改成功");
              this.close();
              this.$emit('success');
            }).catch(() => {
              this.submitting = false;
            });
          } else {
            addType(this.form).then(response => {
              this.submitting = false;

              this.msgSuccess("新增成功");
              this.close();
              this.$emit('success');
            }).catch(() => {
              this.submitting = false;
            });
          }
        }
      });
    },
    // 表单重置
    reset() {
      this.form = {
        dictName: undefined,
        dictType: undefined,
        status: "0",
        remark: undefined
      };
      this.resetForm("form");
    },
  }
}
</script>

<style scoped>

</style>
